//
// Created by Administrator on 2021/10/20.
//
#include "iostream"
#include "anyList.h"

using namespace std;


void AnyList::add(Node *node)
{
    node->setNext(first);
    first = node;
    count++;
}

AnyList::AnyList()
{
    first = nullptr;
    count = 0;
}

ostream& operator << (ostream& output,const AnyList& c)
{
    if (c.first == nullptr || c.count == 0)
    {
        output << "List is empty." << endl;
        return output;
    }
    else
    {
        output << c.first->getData();
        Node* temp = c.first->getNext();
        while (temp != nullptr)
        {
            output << " " << temp->getData();
            temp = temp->getNext();
        }
        output << endl;
        return output;
    }
}

AnyList::AnyList(const AnyList &obj)
{
    count = obj.count;
    if (obj.first == nullptr)
    {
        first = nullptr;
    }
    else
    {
        first = new Node(obj.first->getData(), nullptr);
        Node* temp2 = obj.first->getNext();
        Node* temp1 = first;
        while (temp2 != nullptr)
        {
            temp1->setNext(new Node(temp2->getData(), nullptr));
            temp1 = temp1->getNext();
            temp2 = temp2->getNext();
        }
        temp1 = temp1->getNext();
    }
}

bool AnyList::operator == (const AnyList &rhs) const
{
    if (count != rhs.count)
    {
        return false;
    }
    else
    {
        Node* node1 = first;
        Node* node2 = rhs.first;
        while (node1 != nullptr && node2 != nullptr)
        {
            if (node1->getData() != node2->getData())
            {
                return false;
            }
            node1 = node1->getNext();
            node2 = node2->getNext();
        }
        if (node1 == nullptr && node2 == nullptr)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

void AnyList::swapValues(AnyList &test) {
    Node *temp = first;
    while (temp->getNext() != nullptr) {
        temp = temp->getNext();
    }
    int value = temp->getData();
    temp->setData(test.first->getNext()->getData());
    test.first->getNext()->setData(value);
}

void AnyList::insertSum(const AnyList& test) {
    if (test.first->getNext() == nullptr) {
        return;
    }
    Node *temp = first;
    while (temp->getNext() != nullptr) {
        temp = temp->getNext();
    }
    temp->setNext(new Node
                          (test.first->getData() + test.first->getNext()->getData(),
                           nullptr));
    count++;
}








